/* Includes ------------------------------------------------------------------*/
#include "status_bar.h"
#include "string.h"
#include "main.h"
#include "stdlib.h"

/* Private macro -------------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/******************************************************************************
 * Function Name  : status_bar_init
 * Description    : init status bar
 * Input          : [status_bar_t *status_bar] status bar object
 * Output         : None
 * Return         : None
 *******************************************************************************/
void status_bar_init(status_bar_t *status_bar)
{
    status_bar->box_w = UI_STATUS_BAR_WIDTH;
    status_bar->box_h = UI_STATUS_BAR_HEIGHT;
    status_bar->icon_w = 0;
    status_bar->icon_h = 0;
    status_bar->icon_head = NULL;
    status_bar->box = NULL;
    status_bar->time = NULL;
}

/******************************************************************************
 * Function Name  : status_bar_create
 * Description    : create status bar
 * Input          : [status_bar_t *status_bar] status bar object
 *                : [lv_obj_t *parent] parent object
 * Output         : None
 * Return         : [status_bar_t *] status bar object
 *******************************************************************************/
status_bar_t *status_bar_create(status_bar_t *status_bar, lv_obj_t *parent)
{
    status_bar->box = lv_obj_create(parent);
    lv_obj_set_size(status_bar->box, status_bar->box_w, status_bar->box_h);
    lv_obj_set_style_bg_color(status_bar->box, lv_color_hex(0x000000), 0);
    lv_obj_set_style_bg_opa(status_bar->box, LV_OPA_COVER, 0);
    lv_obj_align_to(status_bar->box, parent, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_set_style_radius(status_bar->box, 0, 0);
    lv_obj_set_style_border_width(status_bar->box, 0, 0);
    lv_obj_set_style_pad_ver(status_bar->box, 0, 0);
    lv_obj_set_style_pad_hor(status_bar->box, 10, 0);

    lobj[LID_BAR_TIME] = lv_label_create(status_bar->box);
    lv_obj_set_style_text_color(lobj[LID_BAR_TIME], lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(lobj[LID_BAR_TIME], device.ui.fnt.size_18.font, 0);
    lv_obj_align(lobj[LID_BAR_TIME], LV_ALIGN_LEFT_MID, 0, 0);
    time_t rawtime = time(NULL);
    struct tm *Cur_time = localtime(&rawtime);
    static char Time[64];
    strftime(Time, sizeof(Time), "%Y-%m-%d    %H:%M:%S", Cur_time);
    lv_label_set_text(lobj[LID_BAR_TIME], Time);

    status_bar->icon_head = malloc(sizeof(status_icon_t));
    memset(status_bar->icon_head, 0, sizeof(status_icon_t));

    return status_bar;
}

/******************************************************************************
 * Function Name  : status_bar_set_icon
 * Description    : set status bar icon
 * Input          : [status_bar_t *status_bar, string name, string content] status bar object, icon name, icon content
 * Output         : None
 * Return         : None
 * Note           : if the icon does not exist, it will be created
 ******************************************************************************/
void status_bar_set_icon(status_bar_t *status_bar, string name, string content)
{
    status_icon_t *icon = status_bar->icon_head;
    if (strlen(name) >= STATUS_BAR_ICON_NAME_LEN_MAX)
    {
        name[STATUS_BAR_ICON_NAME_LEN_MAX - 1] = '\0';
    }

    if (strlen(content) >= STATUS_BAR_ICON_CONTENT_LEN_MAX)
    {
        content[STATUS_BAR_ICON_CONTENT_LEN_MAX - 1] = '\0';
    }

    while (icon->next != NULL)
    {
        if (strcmp(icon->name, name) == 0)
        {
            lv_label_set_text(icon->icon, content);
            return;
        }
        icon = icon->next;
    }
    if (strcmp(icon->name, name) == 0)
    {
        lv_label_set_text(icon->icon, content);
        return;
    }

    icon->next = malloc(sizeof(status_icon_t));
    memset(icon->next, 0, sizeof(status_icon_t));
    icon = icon->next;

    icon->icon = lv_label_create(status_bar->box);
    lv_obj_set_style_text_color(icon->icon, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(icon->icon, device.ui.fnt.size_18.font, 0);
    lv_label_set_text(icon->icon, content);
    lv_obj_align_to(icon->icon, status_bar->box, LV_ALIGN_RIGHT_MID, 0, 0);
    strcpy(icon->name, name);
    strcpy(icon->content, content);
}

/******************************************************************************
 * Function Name  : status_bar_refresh_thread
 * Description    :  refresh status_bar
 * Input          : None
 * Output         : None
 * Return         : None
 * Note           :
 ******************************************************************************/
// void *status_bar_refresh_thread(status_bar_t *status_bar)
// {
//     while(true)
//     {
//         time_t rawtime = time(NULL);
//         struct tm *Cur_time = localtime(&rawtime);
//         static char Time[12];
//         strftime(Time, sizeof(Time), "%H:%M", Cur_time);
//         lv_label_set_text(status_bar->time, Time);
//         sleep(5);
//     }
// }

/******************************************************************************
 * Function Name  : status_bar_refresh
 * Description    :  refresh status_bar
 * Input          : None
 * Output         : None
 * Return         : None
 * Note           :
 ******************************************************************************/
// void status_bar_refresh(status_bar_t *status_bar)
// {
//     time_t rawtime = time(NULL);
//     struct tm *Cur_time = localtime(&rawtime);
//     static char Time[12];
//     strftime(Time, sizeof(Time), "%H:%M", Cur_time);
//     lv_label_set_text(status_bar->time, Time);
// }

/************************ (C) COPYRIGHT PURESUN *****END OF FILE***************/
